package cn.hanyx.vlog.articleservice.feignclient.fallback;

import cn.hanyx.vlog.articleservice.dto.ResponseObject;
import cn.hanyx.vlog.articleservice.entity.Video;
import cn.hanyx.vlog.articleservice.feignclient.VideoFeignClient;
import feign.hystrix.FallbackFactory;
import org.springframework.stereotype.Component;

/**
 * OpenFeign Fallback Factory书写规则
 * 1. 实现FallbackFactory接口,且泛型为对应的FeignClient
 * 2. 增加@Component让Spring对其实例化并IOC管理
 * 3. 在create方法中实现FeignClient,说明限流/熔断时的操作
 */
@Component
public class VideoFeignClientFallbackFactory implements FallbackFactory<VideoFeignClient> {

    @Override
    public VideoFeignClient create(Throwable throwable) {
        return new VideoFeignClient() {
            @Override
            public ResponseObject<Video> findByArticleId(Long articleId) {
                String code = throwable.getClass().getSimpleName();
                System.err.println("触发异常:" + code);
                /*
                可根据throwable判断是限流还是熔断
                 */
                throwable.printStackTrace();
                Video video = new Video();
                video.setVideoId(-1L);
                video.setSn("UNKNOWN");
                return new ResponseObject<>(code, "线路或熔断", video);
            }
        };

    }
}
